<?php
#+------------------------------------------------------------------
#| 我曾七次鄙视自己的灵魂:
#| 第一次,当它本可进取时，却故作谦卑；
#| 第二次,当它空虚时，用爱欲来填充；
#| 第三次,在困难和容易之间，它选择了容易；
#| 第四次,它犯了错，却借由别人也会犯错来宽慰自己；
#| 第五次,它自由软弱，却把它认为是生命的坚韧；
#| 第六次,当它鄙夷一张丑恶的嘴脸时，却不知那正是自己面具中的一副；
#| 第七次,它侧身于生活的污泥中虽不甘心，却又畏首畏尾。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\user\controller;

use app\common\model\VisitLog;
use app\user\model\ShortUrlLog;

class Visit extends Ucenter
{
    public function info(int $id = 0, int $times = 0)
    {
        //今天、昨天、近7日、最近30天
        $short = ShortUrlLog::find($id);
        $query = VisitLog::where('url_id', $id);
        $labels = [];
        $timesButton = [
            '今日','昨日','近7日','近30日'
        ];
        switch ($times) {
            case 0:
                $query->whereDay('create_time');
                for ($i = 0; $i < 24; $i++) {
                    $labels[$i] = 0;
                }
                break;
            case 1:
                $query->whereDay('create_time', 'yesterday');
                for ($i = 0; $i < 24; $i++) {
                    $labels[$i] = 0;
                }
                break;
            case 2:
                $begin_time = strtotime(date('Y-m-d')) - 7 * 86400;
                $end_time = strtotime(date('Y-m-d 23:59:59'));
                $query->whereBetweenTime('create_time',$begin_time, $end_time);
                for ($begin_time; $begin_time < $end_time; $begin_time += 86400) {
                    $labels[date('m-d', $begin_time)] = 0;
                }
                break;
            case 3:
                $begin_time = strtotime(date('Y-m-d')) - 30 * 86400;
                $end_time = strtotime(date('Y-m-d 23:59:59'));
                $query->whereBetweenTime('create_time',$begin_time, $end_time);
                for ($begin_time; $begin_time < $end_time; $begin_time += 86400) {
                    $labels[date('m-d', $begin_time)] = 0;
                }
                break;
        }
        $visitQuery = clone $query;
        $ipQuery = clone $query;
        $norefererQuery = clone $query;
        $visitLists = $query->order('id,create_time', 'desc')->select();
        if ($visitLists) {
            $visitLists = $visitLists->toArray();
        }

        $dayGroupCount = $devicesList = $cityList = $provinceList = [];
        foreach ($visitLists as $value) {
            if($times <= 1){
                $key = date('H', strtotime($value['create_time']));
            }else{
                $key = date('m-d', strtotime($value['create_time']));
            }
            $dayGroupCount[$key][] = $value;
            $devicesList[$value['device']][] = $value;
            $cityList[$value['city']==""?'未知':$value['city']][] = $value;
            $provinceList[$value['province']==""?'未知':$value['province']][] = $value;
        }

        $dayGroupCount = array_map('count', $dayGroupCount);
        $devicesList = array_map('count', $devicesList);
        $cityList = array_map('count', $cityList);
        $provinceList = array_map('count', $provinceList);
        $dayGroupCount = $dayGroupCount+$labels;
        ksort($dayGroupCount);
        $cityColor = array_map('randColor',$cityList);
        $deviceColor = array_map('randColor',$devicesList);
        $provinceColor = array_map('randColor',$provinceList);
        $dayLabel = array_map(function($item)use($times){
            if($times < 2){
                return $item . '时';
            }
            return $item.'日';
        },array_keys($labels));
        return $this->fetch('', [
            'day'    => json_encode(array_values($dayGroupCount),JSON_UNESCAPED_UNICODE),
            'label'  => json_encode($dayLabel,JSON_UNESCAPED_UNICODE),
            'device' => json_encode(array_values($devicesList),JSON_UNESCAPED_UNICODE),
            'device_label' => json_encode(array_keys($devicesList),JSON_UNESCAPED_UNICODE),
            'device_color'   => json_encode(array_values($deviceColor),JSON_UNESCAPED_UNICODE),
            'city'   => json_encode(array_values($cityList),JSON_UNESCAPED_UNICODE),
            'city_label'   => json_encode(array_keys($cityList),JSON_UNESCAPED_UNICODE),
            'city_color'   => json_encode(array_values($cityColor),JSON_UNESCAPED_UNICODE),
            'province'   => json_encode(array_values($provinceList),JSON_UNESCAPED_UNICODE),
            'province_label'   => json_encode(array_keys($provinceList),JSON_UNESCAPED_UNICODE),
            'province_color'   => json_encode(array_values($provinceColor),JSON_UNESCAPED_UNICODE),
            'times_area' => $timesButton,
            'short' => $short,
            'all_visit' => $visitQuery->count(),
            'ip_num' => $ipQuery->group('ip')->count('id'),
            'no_referer'  => $norefererQuery->where('referer','')->count()
        ]);
    }

    public function chars()
    {
        return '';
        return $this->fetch();
    }
}